<template>
    <div v-loading="loading">
        <el-drawer v-model="drawerVisiable" :destroy-on-close="true" :close-on-click-modal="false" size="50%">
            <template #header>
                <DrawerHeader :header="$t('terminal.host')" :back="handleClose" />
            </template>
            <el-row type="flex" justify="center">
                <el-col :span="22">
                    <el-form ref="hostInfoRef" label-position="top" :model="dialogData.rowData" :rules="rules">
                        <el-form-item :label="$t('terminal.ip')" prop="addr">
                            <span v-if="dialogData.rowData!.addr === '127.0.0.1' && dialogData.title === 'edit'">
                                {{ dialogData.rowData!.addr }}
                            </span>
                            <el-input v-else clearable v-model.trim="dialogData.rowData!.addr" />
                        </el-form-item>
                        <el-form-item :label="$t('terminal.user')" prop="user">
                            <el-input clearable v-model="dialogData.rowData!.user" />
                        </el-form-item>
                        <el-form-item :label="$t('terminal.authMode')" prop="authMode">
                            <el-radio-group v-model="dialogData.rowData!.authMode">
                                <el-radio label="password">{{ $t('terminal.passwordMode') }}</el-radio>
                                <el-radio label="key">{{ $t('terminal.keyMode') }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item
                            :label="$t('terminal.password')"
                            v-if="dialogData.rowData!.authMode === 'password'"
                            prop="password"
                        >
                            <el-input clearable show-password type="password" v-model="dialogData.rowData!.password" />
                        </el-form-item>
                        <el-form-item
                            :label="$t('terminal.key')"
                            v-if="dialogData.rowData!.authMode === 'key'"
                            prop="privateKey"
                        >
                            <el-input clearable type="textarea" v-model="dialogData.rowData!.privateKey" />
                        </el-form-item>
                        <el-form-item
                            :label="$t('terminal.keyPassword')"
                            v-if="dialogData.rowData!.authMode === 'key'"
                            prop="passPhrase"
                        >
                            <el-input
                                type="password"
                                show-password
                                clearable
                                v-model="dialogData.rowData!.passPhrase"
                            />
                        </el-form-item>
                        <el-checkbox clearable v-model.number="dialogData.rowData!.rememberPassword">
                            {{ $t('terminal.rememberPassword') }}
                        </el-checkbox>
                        <el-form-item style="margin-top: 10px" :label="$t('terminal.port')" prop="port">
                            <el-input clearable v-model.number="dialogData.rowData!.port" />
                        </el-form-item>
                        <el-form-item :label="$t('commons.table.group')" prop="groupID">
                            <el-select filterable v-model="dialogData.rowData!.groupID" clearable style="width: 100%">
                                <el-option
                                    v-for="item in groupList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('commons.table.title')" prop="name">
                            <el-input clearable v-model="dialogData.rowData!.name" />
                        </el-form-item>
                        <el-form-item :label="$t('commons.table.description')" prop="description">
                            <el-input clearable type="textarea" v-model="dialogData.rowData!.description" />
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="drawerVisiable = false">{{ $t('commons.button.cancel') }}</el-button>
                    <el-button @click="submitAddHost(hostInfoRef, 'testconn')">
                        {{ $t('terminal.testConn') }}
                    </el-button>
                    <el-button type="primary" :disabled="!isOK" @click="submitAddHost(hostInfoRef, dialogData.title)">
                        {{ $t('commons.button.confirm') }}
                    </el-button>
                </span>
            </template>
        </el-drawer>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import type { ElForm } from 'element-plus';
import { Rules } from '@/global/form-rules';
import { addHost, editHost, testByInfo } from '@/api/modules/host';
import { GetGroupList } from '@/api/modules/group';
import i18n from '@/lang';
import { MsgError, MsgSuccess } from '@/utils/message';

const loading = ref();
const isOK = ref(false);
interface DialogProps {
    title: string;
    rowData?: any;
    getTableList?: () => Promise<any>;
}
const title = ref<string>('');
const drawerVisiable = ref(false);
const dialogData = ref<DialogProps>({
    title: '',
});

watch(
    () => dialogData.value.rowData,
    () => {
        isOK.value = false;
    },
    { deep: true },
);

const groupList = ref();
const acceptParams = (params: DialogProps): void => {
    dialogData.value = params;
    title.value = i18n.global.t('commons.button.' + dialogData.value.title);
    drawerVisiable.value = true;
    loadGroups();
};
const emit = defineEmits<{ (e: 'search'): void }>();
const handleClose = () => {
    drawerVisiable.value = false;
};

type FormInstance = InstanceType<typeof ElForm>;
const hostInfoRef = ref<FormInstance>();
const rules = reactive({
    groupID: [Rules.requiredSelect],
    addr: [Rules.host],
    port: [Rules.requiredInput, Rules.port],
    user: [Rules.requiredInput],
    authMode: [Rules.requiredSelect],
});

const loadGroups = async () => {
    const res = await GetGroupList({ type: 'host' });
    groupList.value = res.data;
    if (dialogData.value.title === 'create') {
        for (const item of groupList.value) {
            if (item.isDefault) {
                dialogData.value.rowData.groupID = item.id;
                break;
            }
        }
    }
};

const submitAddHost = (formEl: FormInstance | undefined, ops: string) => {
    if (!formEl) return;
    formEl.validate(async (valid) => {
        if (!valid) return;
        if (ops === 'create') {
            loading.value = true;
            await addHost(dialogData.value.rowData)
                .then(() => {
                    loading.value = false;
                    MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
                    drawerVisiable.value = false;
                    emit('search');
                })
                .catch(() => {
                    loading.value = false;
                });
        }
        if (ops === 'edit') {
            loading.value = true;
            await editHost(dialogData.value.rowData)
                .then(() => {
                    loading.value = false;
                    MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
                    drawerVisiable.value = false;
                    emit('search');
                })
                .catch(() => {
                    loading.value = false;
                });
        }
        if (ops === 'testconn') {
            loading.value = true;
            await testByInfo(dialogData.value.rowData).then((res) => {
                loading.value = false;
                if (res.data) {
                    isOK.value = true;
                    MsgSuccess(i18n.global.t('terminal.connTestOk'));
                } else {
                    isOK.value = false;
                    MsgError(i18n.global.t('terminal.connTestFailed'));
                }
            });
        }
    });
};

defineExpose({
    acceptParams,
});
</script>
